﻿using System.Windows;
using System.Windows.Media;
using MyFloridaHouse.libSvBingMapsMultiShape;
using Microsoft.Maps.MapControl;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
using MyFloridaHouse.libSvOpenDistricts.Districts;
using System.Collections.Generic;
using System;

namespace MyFloridaHouse.libSvOpenDistricts.MapShapes
{

    public partial class Region : IDisposable
    {

        public int regionID;
        private Location centerLocation;
        private MapLayer myBingMapLayer;

        public string toolTipText;
        public MultiMapShapeBase regionShape;
        public BoundingBoxDTO boundingBox { get; set; }
        public MapShapeLayer layer;  //TODO:  I think we can change this to layerID...
        public RegionDetails details;

        private UIElement labelShape;

        public List<RegionDataDTO> dataList;

        public Region(int regionID, MapLayer myBingMapLayer, Location centerLocation, BoundingBoxDTO boundingBox)
        {
            this.regionID = regionID;
            this.myBingMapLayer = myBingMapLayer;
            this.centerLocation = centerLocation;
            this.boundingBox = boundingBox;
            this.regionShape = new MapMultiPolygon();
            this.dataList = new List<RegionDataDTO>();
            toolTipText = regionID.ToString();
            SubscribeEvents();
        }

        public void Dispose()
        {
            UnsubscribeEvents();
        }

        private void SubscribeEvents()
        {
            RegionFill.RegionFillConditionsChanged += OnRegionFillConditionsChanged;
            RegionLabel.RegionLabelConditionsChanged += OnRegionLabelConditionsChanged;
        }

        private void UnsubscribeEvents()
        {
            RegionFill.RegionFillConditionsChanged -= OnRegionFillConditionsChanged;
            RegionLabel.RegionLabelConditionsChanged -= OnRegionLabelConditionsChanged;        
        }

        private void OnRegionFillConditionsChanged(object sender, EventArgs args)
        {
                ReplaceFillColor();
        }

        private void OnRegionLabelConditionsChanged(object sender, EventArgs args)
        {
            ReplaceLabelShape();
        }

        public override string ToString()
        {
            return regionID.ToString();
        }

        public void Hide()
        {
            if (regionShape != null) regionShape.Visibility = Visibility.Collapsed;
            if (labelShape != null) labelShape.Visibility = Visibility.Collapsed;
        }

        public void Show()
        {
            if (regionShape != null) regionShape.Visibility = Visibility.Visible;
            if (labelShape != null) labelShape.Visibility = Visibility.Visible;
        }

        public void Select()
        {
            if (regionShape != null)
            {
                regionShape.Stroke = details.selected.stroke;
                regionShape.StrokeThickness = details.selected.strokeThickness;
            }
        }

        public void Unselect()
        {
            if (regionShape != null)
            {
                regionShape.Stroke = details.unselected.stroke;
                regionShape.StrokeThickness = details.unselected.strokeThickness;
            }
        }


        internal void AddShape(MultiMapShapeBase iRegionShape)
        {
            if (regionShape != null)
            {
                regionShape = iRegionShape;
                regionShape.BorderBrush = details.unselected.stroke;
                regionShape.Visibility = Visibility.Collapsed;
                myBingMapLayer.Children.Add(regionShape);
                ReplaceLabelShape();
                ReplaceFillColor();
            }
        }

        public void ReplaceData(List<RegionDataDTO> regionDataList)
        {
            if (regionDataList != null)
            {
                dataList.Clear();
                dataList.AddRange(regionDataList);
                ReplaceLabelShape();
                ReplaceFillColor();
            }
        }

        private void ReplaceLabelShape()
        {
            //need to remove the old label shape before we can add a new one
            myBingMapLayer.Children.Remove(labelShape);

            labelShape = RegionLabel.regionLabelInstance.GetRegionLabelShape(this.dataList);

            if (labelShape != null)
            {

                labelShape.Visibility = Visibility.Collapsed;
                if (regionShape.Visibility == Visibility.Visible)
                {
                    labelShape.Visibility = Visibility.Visible;
                }
                // Set tool tip on text since it's above the shape.
                //ToolTipService.SetToolTip(label, "This is a label tooltip");
                myBingMapLayer.AddChild(labelShape, centerLocation, PositionOrigin.Center);
            }

        }

        private void ReplaceFillColor()
        {
            if (layer.isFillByRegionIndexType)
            {
                SolidColorBrush fillColorBrush = DistrictColors.GetBrushFromIndex(this.regionID);
                if (fillColorBrush != null)
                {
                    regionShape.Fill = fillColorBrush;
                }
            }
            else
            {
                SolidColorBrush fillColorBrush = RegionFill.regionFillInstance.GetRegionFillColorBrush(this.dataList);
                if (fillColorBrush != null)
                {
                    regionShape.Fill = fillColorBrush;
                }
            }
        }


    }


}
